package com.coupons.business.common.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.coupons.business.common.vo.MerchantOffer;

public class OfferList extends ArrayList<MerchantOffer>
{
	private static final long serialVersionUID = 5605306392641396047L;
	
	private String compareKey="homePageOffer";
	public OfferList(List<MerchantOffer> lstOffers)
	{
				
		this.addAll(lstOffers);
		Collections.sort(lstOffers, new OffersComparator() );
		
		
//		for (Iterator<MerchantOffer> iterator = lstOffers.iterator(); iterator.hasNext();)
//		{
//			MerchantOffer merchantOffer =  iterator.next();
//			this.put(new Integer(merchantOffer.getHomePageOfferIndex()), merchantOffer);
//		}
		
	}
	
	
	public OfferList(List<MerchantOffer> lstOffers,String straCompareKey)
	{
		this.compareKey=straCompareKey;	
		this.addAll(lstOffers);
		Collections.sort(lstOffers, new OffersComparator() );
		
		
//		for (Iterator<MerchantOffer> iterator = lstOffers.iterator(); iterator.hasNext();)
//		{
//			MerchantOffer merchantOffer =  iterator.next();
//			this.put(new Integer(merchantOffer.getHomePageOfferIndex()), merchantOffer);
//		}
		
	}
	
	
	
	class OffersComparator implements java.util.Comparator<MerchantOffer> {
		
		  public int compare(MerchantOffer offer1, MerchantOffer offer2) {
			    
			  if("homePageOffer".equalsIgnoreCase(compareKey))
			  {
				  if(offer1.getHomePageOfferIndex()<offer2.getHomePageOfferIndex())
				  		return -1;
				  	else if(offer1.getHomePageOfferIndex()==offer2.getHomePageOfferIndex())
				  		return 0;
				  	else if (offer1.getHomePageOfferIndex()> offer2.getHomePageOfferIndex())
				  		return 1;
				  	return 0;
			  }	  
			  else if("merchantPageOffer".equalsIgnoreCase(compareKey))
			  {
				  if(offer1.getMerchantPageOfferIndex()<offer2.getMerchantPageOfferIndex())
				  		return -1;
				  	else if(offer1.getMerchantPageOfferIndex()==offer2.getMerchantPageOfferIndex())
				  		return 0;
				  	else if (offer1.getMerchantPageOfferIndex()> offer2.getMerchantPageOfferIndex())
				  		return 1;
				  	return 0; 
			  }
			  else return 0;
		  }
	}
}
